package xin.alum.aim.redis;

import lombok.SneakyThrows;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import xin.alum.aim.groups.Session;
import xin.alum.aim.groups.Sessions;

/**
 * 集群环境下，监控多设备登录情况，控制是否其余终端下线的逻辑
 */
class KickMessageListener implements MessageListener {

   private final Sessions sessions;

   private final Jackson2JsonRedisSerializer<Object> json;

    KickMessageListener(Sessions sessions,Jackson2JsonRedisSerializer<Object> json) {
        this.sessions=sessions;
        this.json=json;
    }

    @SneakyThrows
    @Override
    public void onMessage(org.springframework.data.redis.connection.Message redisMessage, byte[] bytes) {
        Session s = (Session) json.deserialize(redisMessage.getBody());
        if (s != null) {
            sessions.kick(s);
        }
    }
}
